<?php

$monthlyCron = mysql_query("SELECT * FROM ghetto_cron WHERE game = '$game' AND monthly_run = '$today' AND last_run != '$today2'");
while ($getMonthlyCron = mysql_fetch_array($monthlyCron))
{
	$explodeCronTime = explode(":", $getMonthlyCron[cron_time]);
	$cronHour  = $explodeCronTime[0];
	$cronMin   = $explodeCronTime[1];
	if (($cronHour <= $hour) AND ($cronMin <= $minute))
	{
		$cronName = $getMonthlyCron[cron_name];
		$run[$cronName] = 1;
		mysql_query("UPDATE ghetto_cron SET last_run = '$today2' WHERE game = '$game' AND id = '$getMonthlyCron[id]'");
		mysql_query("UPDATE ghetto_cron SET total_runs = total_runs + 1 WHERE game = '$game' AND id = '$getMonthlyCron[id]'");
	}
}

$weeklyCron = mysql_query("SELECT * FROM ghetto_cron WHERE game = '$game' AND weekly_run = '$today3' AND last_run != '$today3'");
while ($getWeeklyCron = mysql_fetch_array($weeklyCron))
{
	$explodeCronTime = explode(":", $getWeeklyCron[cron_time]);
	$cronHour  = $explodeCronTime[0];
	$cronMin   = $explodeCronTime[1];
	if (($cronHour <= $hour) AND ($cronMin <= $minute))
	{
		$cronName = $getWeeklyCron[cron_name];
		$run[$cronName] = 1;
		mysql_query("UPDATE ghetto_cron SET last_run = '$today3' WHERE game = '$game' AND id = '$getWeeklyCron[id]'");
		mysql_query("UPDATE ghetto_cron SET total_runs = total_runs + 1 WHERE game = '$game' AND id = '$getWeeklyCron[id]'");
	}
}

$dailyCron = mysql_query("SELECT * FROM ghetto_cron WHERE game = '$game' AND weekly_run = '0' AND monthly_run = '0' AND last_run != '$today2'");
while ($getDailyCron = mysql_fetch_array($dailyCron))
{
	$explodeCronTime = explode(":", $getDailyCron[cron_time]);
	$cronHour  = $explodeCronTime[0];
	$cronMin   = $explodeCronTime[1];
	if (($cronHour <= $hour) AND ($cronMin <= $minute))
	{
		$cronName = $getDailyCron[cron_name];
		$run[$cronName] = 1;
		mysql_query("UPDATE ghetto_cron SET last_run = '$today2' WHERE game = '$game' AND id = '$getDailyCron[id]'");
		mysql_query("UPDATE ghetto_cron SET total_runs = total_runs + 1 WHERE game = '$game' AND id = '$getDailyCron[id]'");
	}
}

if ($run['Low Stock'])
{
	mysql_query("UPDATE shop_items2 SET cur_stock = (max_stock / 3) WHERE game = '$game'");
}
if ($run['Medium Stock'])
{
	mysql_query("UPDATE shop_items2 SET cur_stock = (max_stock / 2) WHERE game = '$game'");
}
if ($run['Full Stock'])
{
	mysql_query("UPDATE shop_items2 SET cur_stock = max_stock WHERE game = '$game'");
}
if ($run['Lower Pet Attitude'])
{
	mysql_query("UPDATE user_pets2 SET attitude = attitude - 1 WHERE game = '$game' AND attitude > '0'");
}
if ($run['Lower Pet Hunger'])
{
	mysql_query("UPDATE user_pets2 SET hunger = hunger - 1 WHERE game = '$game' AND hunger > '0'");
}
if ($run['Lower User Hunger'])
{
	mysql_query("UPDATE members2 SET hunger_level = hunger_level - 1 WHERE game = '$game' AND hunger_level > '0'");
}
if ($run['End Penny Play'])
{
	$numEntries = fetch("SELECT sum(entries) AS entries FROM penny_play2 WHERE game = '$game'");

	$findRow = mysql_query("SELECT userid,entries FROM penny_play2 WHERE game = '$game'");
	$winningNumber = rand(1,$numEntries[entries]);
	$x = 0;
	while ($getRow = mysql_fetch_array($findRow))
	{
		$userid2 = $getRow[userid];
		$min[$userid2] = $x+1;
		$max[$userid2] = $x+$getRow[entries];
		$x = $x + $getRow[entries];
		if (($min[$userid2] <= $winningNumber) AND ($winningNumber <= $max[$userid2]))
		{
			$winner = $userid2;
		}
	}

	$findWinner = fetch("SELECT username,points,id FROM members2 WHERE id = '$winner' AND 	game = '$game'");
	$winnings = round($numEntries[entries] * .95);
	points ("$game", $findWinner[username], $winnings);
	systemmessage("$game", "", "$findWinner[id]", "Congratulations! You have won $winnings 	$pointsVar from the Penny Play!!");
	mysql_query("DELETE FROM penny_play2 WHERE game = '$game'");
}
if ($run['End Referral Contest'])
{
	$place = "First";

	$findTop3 = mysql_query("SELECT username,referrals2 FROM members_profiles2 WHERE username 	!= 'topwebgames' AND game = '$game' ORDER BY referrals2 DESC LIMIT 3");
	$place = "First";

	while ($getTop3 = mysql_fetch_array($findTop3))
	{
		if ($place == "First") { $winnings = 50000; }
		if ($place == "Second") { $winnings = 25000; }
		if ($place == "Third") { $winnings = 12500; }

		points ("$game", $getTop3[username], $winnings);
		$text .= "$place place goes to $getTop3[username] with $getTop3[referrals2] 	referrals! They won $winnings $pointsVar!<Br>";

		if ($place == "Second") { $place = "Third"; }
		if ($place == "First") { $place = "Second"; }
	}

	mysql_query("UPDATE members_profiles2 SET referrals2=0 WHERE game = '$game'");
}
if ($run['End Daily Lotto'])
{
	###############
	####Daily Three

	$three[1] = rand(1,9);
	$three[2] = rand(1,9);
	$three[3] = rand(1,9);

	$three[tickets] = mysql_query("SELECT user FROM daily_three2 WHERE game = '$game'");
	$three[winners] = mysql_query("SELECT user FROM daily_three2 WHERE first = '$three[1]' 	AND second = '$three[2]' AND third = '$three[3]' AND game = '$game'");
	$three[num_tickets] = mysql_num_rows($three[tickets]);
	$three[num_winners] = mysql_num_rows($three[winners]);
	if ($three[num_winners] == 0)
	{
		$three[num_winners] = 1;
	}
	$three[jackpot] = $three[num_tickets] * 65;
	$three[prize] = round($three[jackpot] / $three[num_winners]);

	$three[list_winners] = "Winners:";

	while ($array = mysql_fetch_array($three[winners]))
	{
		$fetch = fetch("SELECT username,points FROM members2 WHERE id = '$array[user]' AND 	game = '$game'");
		mysql_query("UPDATE members2 SET points=$fetch[points]+$three[prize] WHERE id = 	'$array[user]' AND game = '$game'");
		$three[list_winners] .= "<br>$fetch[username]";
		systemmessage("$game", "", "$array[user]", "Congratulations! You have won 	$three[prize] $pointsVar from the Daily 3!!");
	}

	if ($three[list_winners] == "Winners:")
	{
		$three[list_winners] = "There were no winners. The $three[jackpot] $pointsVar will be 	carried over to the next lottery.";
	}
	else
	{
		$three[list_winners] = "The $three[jackpot] $pointsVar was split between 	$three[num_winners] users, each receiving $three[prize] $pointsVar.<br>$three[list_winners]";
		mysql_query("DELETE FROM daily_three2 WHERE game = '$game'");
	}

	###############
	#####Daily Five

	$five[1] = rand(1,9);
	$five[2] = rand(1,9);
	$five[3] = rand(1,9);
	$five[4] = rand(1,9);
	$five[5] = rand(1,9);

	$five[tickets] = mysql_query("SELECT user FROM daily_five2 WHERE game = '$game'");
	$five[winners] = mysql_query("SELECT user FROM daily_five2 WHERE first = '$five[1]' AND 	second = '$five[2]' AND third = '$five[3]' AND fourth = '$five[4]' AND fifth = '$five[5]' AND 	game = '$game'");
	$five[num_tickets] = mysql_num_rows($five[tickets]);
	$five[num_winners] = mysql_num_rows($five[winners]);
	if ($five[num_winners] == 0)
	{
		$five[num_winners] = 1;
	}
	$five[jackpot] = $five[num_tickets] * 99;
	$five[prize] = round($five[jackpot] / $five[num_winners]);

	$five[list_winners] = "Winners:";

	while ($array = mysql_fetch_array($five[winners]))
	{
		$fetch = fetch("SELECT username,points FROM members2 WHERE id = '$array[user]' AND 	game = '$game'");
		mysql_query("UPDATE members2 SET points=$fetch[points]+$five[prize] WHERE id = 	'$array[user]' AND game = '$game'");
		$five[list_winners] .= "<br>$fetch[username]";
		systemmessage("$game", "", "$array[user]", "Congratulations! You have won 	$five[prize] $pointsVar from the Daily 5!!");
	}

	if ($five[list_winners] == "Winners:")
	{
		$five[list_winners] = "There were no winners. The $five[jackpot] $pointsVar will be 	carried over to the next lottery.";
	}
	else
	{
		$five[list_winners] = "The $five[jackpot] $pointsVar was split between 	$five[num_winners] users, each receiving $five[prize] $pointsVar.<br>$five[list_winners]";
		mysql_query("DELETE FROM daily_five2 WHERE game = '$game'");
	}
}

?>